using System;
using System.Collections.Generic;
using TTXNA.src.Engine.Graphics;
using TTXNA.src.Util;
using Microsoft.Xna.Framework;
using System.Collections;
using TTXNA.src.Engine.World;

namespace TTXNA.src.Engine
{
    public class Building : GameObject
    {
        protected bool isBuilt;
        protected BuildingInfo buildingInfo;
        protected List<Coordinate> areaCovered;

        public Building(Vector3 location) : base(location)
        {
            
        }

        protected void SetBuildingArea(List<Coordinate> area)
        {
            areaCovered = area;
        }

        protected void SetupMesh(Coordinate coord, RotateType rotType, ObjectType type)
        {
            float meshX = -1;
            float meshZ = -1;

            if (rotType == RotateType.Zero || rotType == RotateType.OneEighty)
            {
                meshX = coord.X + buildingInfo.Dimensions.X / 2.0f - 0.5f;
                meshZ = coord.Z + buildingInfo.Dimensions.Y / 2.0f - 0.5f;
            }
            else
            {
                meshX = coord.X + buildingInfo.Dimensions.X / 2.0f - 0.5f;
                meshZ = coord.Z + buildingInfo.Dimensions.Y / 2.0f - 0.5f;
            }

            Vector3 rotation = new Vector3(0, 0, 0);
            if (rotType == RotateType.Ninety) rotation.Y = 90;
            else if (rotType == RotateType.OneEighty) rotation.Y = 180;
            else if (rotType == RotateType.TwoSeventy) rotation.Y = 270;

            //InstancedMesh iMesh = Game.meshManager.CreateMesh(buildingInfo.MeshPath,
               //                                                 new Vector3(meshX, (Game.gameWorld.Map.GetTileMaxHeight(coord.X, coord.Z)) / Game.heightScale, meshZ), rotation, type);

            //SetMesh(iMesh);
        }
   
        protected bool NotifyTiles()
        {
            bool result = true;
            
            Point dimensions = buildingInfo.Dimensions;

            for (int z = 0; z < dimensions.Y; z++)
            {
                for (int x = 0; x < dimensions.X; x++)
                {
                    if (buildingInfo.SquareOccupied(x, z))
                    {
                        Coordinate coord = new Coordinate(x + (int)Location.X, z + (int)Location.Z);
                        result = result && TTXNAGame.World.Map.AddGameObject(coord, this);
                    }
                }
            }

            isBuilt = result;
            return result;

        }

        public bool IsBuilt
        {
            get { return isBuilt; }
        }
    }

    public struct BuildingInfo
    {
        private string meshPath;
        private Point size;
        private bool[,] occupied;

        public BuildingInfo(string meshPath, Point size, BitArray squares)
        {
            if (size.Y * size.X == squares.Count)
            {
                occupied = new bool[size.X, size.Y];

                for (int z = 0; z < size.Y; z++)
                {
                    for (int x = 0; x < size.X; x++)
                    {
                        occupied[x, z] = squares[z * size.X + x];
                    }
                }

                this.size = size;
                this.meshPath = meshPath;
            }
            else
            {
                throw new Exception("Invalid occupied squares mask");
            }
        }

        public string MeshPath
        {
            get
            {
                return meshPath;
            }
        }

        public Point Dimensions
        {
            get
            {
                return size;
            }
        }

        public bool SquareOccupied(Coordinate coord)
        {
            return occupied[coord.X, coord.Z];
        }

        public bool SquareOccupied(int x, int z)
        {
            return occupied[x, z];
        }

    }
}
